package cn.shyouchi.wuguanjia.android.widget;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Eenie on 2017/2/7 at 13:20
 * Email: 472279981@qq.com
 * Des:
 */

public class SpacesItemDecoration extends RecyclerView.ItemDecoration {

    int space;//间隔宽度，单位px


    public SpacesItemDecoration() {
        space = 0;
    }

    public SpacesItemDecoration(int space) {
        this.space = space;
    }


    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);


    }


    @Override
    public void getItemOffsets(Rect outRect, View itemView, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() instanceof GridLayoutManager) {
            outRect.left = 0;
            outRect.top = 0;
            outRect.right = 0;
            outRect.bottom = 0;
            if (shouldSpaceLeft(parent, itemView)) {
                outRect.left = space / 2;
            }
            if (shouldSpaceTop(parent, itemView)) {
                outRect.top = space / 2;
            }
            if (shouldSpaceRight(parent, itemView)) {
                outRect.right = space/2;
            }
            if (shouldSpaceBottom(parent, itemView)) {
                outRect.bottom = space/2;
            }

        } else {

            if (state.getItemCount() != parent.getChildAdapterPosition(itemView)) {
                outRect.bottom = space;
            }
        }


    }

    private boolean shouldSpaceBottom(RecyclerView parent, View itemView) {
//        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
//        int rowCount = layoutManager.getSpanCount();//行数
//        int itemPos = parent.getChildAdapterPosition(itemView);
//        if (itemPos < rowCount) {
//            return false;
//        } else {
            return true;
//        }
    }

    private boolean shouldSpaceRight(RecyclerView parent, View itemView) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int rowCount = layoutManager.getSpanCount();
        int itemPos = parent.getChildAdapterPosition(itemView);
        if (itemPos % rowCount == rowCount - 1) {
            return false;
        } else {
            return true;
        }
    }

    private boolean shouldSpaceTop(RecyclerView parent, View itemView) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int rowCount = layoutManager.getSpanCount();//行数
        int itemPos = parent.getChildAdapterPosition(itemView);
        if (itemPos < rowCount) {
            return false;
        } else {
            return true;
        }

    }

    private boolean shouldSpaceLeft(RecyclerView parent, View itemView) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int rowCount = layoutManager.getSpanCount();//行数
        int itemPos = parent.getChildAdapterPosition(itemView);
        if (itemPos % rowCount == 0) {
            return false;
        } else {
            return true;
        }
    }


}
